package com.xs.shop.user.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xs.shop.common.PageResult;
import com.xs.shop.common.Result;
import com.xs.shop.user.model.dto.MemberInfoPageQueryDTO;
import com.xs.shop.user.model.entity.MemberInfo;
import com.xs.shop.user.model.vo.MemberInfoPageVO;
import com.xs.shop.user.model.vo.MemberInfoVO;
import com.xs.shop.user.service.service.MemberInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xs
 * @since 2025-07-15
 */
@RestController
@Slf4j
@Tag(name="会员信息管理")
@RequestMapping("/memberInfo")
public class MemberInfoController {
    @Resource
    private MemberInfoService memberInfoService;

    @Operation(summary = "分页查询会员", description = "根据条件分页查询会员信息")
    @GetMapping("page")
    public PageResult<MemberInfoPageVO> page(
            @Parameter(description = "分页查询参数", required = true)
            @ModelAttribute MemberInfoPageQueryDTO memberPageQueryDTO) {
        IPage<MemberInfoPageVO> page = this.memberInfoService.getPage(memberPageQueryDTO);
        return PageResult.success(page);
    }

    @Operation(summary = "根据会员id获取会员信息", description = "根据会员id获取会员信息")
    @GetMapping("{id}")
    public Result<MemberInfo> get(@PathVariable Long id) {
        MemberInfo memberInfo = this.memberInfoService.getById(id);
        return Result.success(memberInfo);
    }

    @Operation(summary = "编辑会员信息", description = "根据ID编辑会员信息")
    @PutMapping("")
    public Result<Object> update(@RequestBody MemberInfo memberInfo) {
        this.memberInfoService.updateById(memberInfo);
        return Result.success();
    }

    @Operation(summary = "批量删除会员", description = "根据ID列表批量删除会员信息")
    @DeleteMapping()
    public Result<Object> delete(@Parameter(description = "会员ID列表", required = true) @RequestBody List<Long> ids) {
        this.memberInfoService.removeBatchByIds(ids);
        return Result.success();
    }


    @Operation(summary = "获取管理员信息", description = "获取当前登录管理员的基本信息")
    @PostMapping("info")
    public Result<MemberInfoVO> info() {
        MemberInfoVO memberInfoVO = this.memberInfoService.info();
        return Result.success(memberInfoVO);
    }

    @Operation(summary = "获取用户的总积分", description = "获取用户的总积分")
    @GetMapping("getMemberTotalPoint")
    public Result<Integer> getMemberTotalPoint(Long memberId){
        log.info("memberId:{}", memberId);
        return Result.success(this.memberInfoService.getMemberTotalPoint(memberId));
    }

    @Operation(summary = "修改会员用户的积分", description = "修改会员用户的积分")
    @GetMapping("updatePointWhenMemberAct")
    public Result<Boolean> updatePointWhenMemberAct(@RequestParam("value") Integer value,
                                                   @RequestParam("status") Integer status){
        log.info("value:{}", value);
        return Result.success(this.memberInfoService.updatePointWhenMemberAct(value, status));
    }

}
